package com.wsd.smartcarwasher.controller;


import com.wsd.smartcarwasher.annotation.Log;
import com.wsd.smartcarwasher.constans.Global;
import com.wsd.smartcarwasher.domain.SubText;
import com.wsd.smartcarwasher.domain.UserInfo;
import com.wsd.smartcarwasher.service.SubTextService;
import com.wsd.smartcarwasher.service.UserInfoService;
import com.wsd.smartcarwasher.service.WxService;
import com.wsd.smartcarwasher.util.ReturnUtil;
import com.wsd.smartcarwasher.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"单页面配置"})
@Controller
@CrossOrigin
public class SubTextController {

    @Resource
    private SubTextService subTextService;
    @Resource
    private WxService wxService;
    @Resource
    private UserInfoService userInfoService;

    @ApiOperation(value = "小程序--关于我们", notes = "关于我们", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/AboutUs", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap AboutUs() {
        List<SubText> ls = subTextService.AboutUs();
        if (ls == null || ls.size() == 0) {
            return ReturnUtil.Success("暂无内容");
        }
        return ReturnUtil.Success("获取成功", ls);
    }


    @ApiOperation(value = "小程序--洗车注意事项", notes = "洗车注意事项", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/washCarTips", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap washCarTips() {
        List<SubText> ls = subTextService.washCarTips();
        if (ls == null || ls.size() == 0) {
            return ReturnUtil.Success("暂无内容");
        }
        return ReturnUtil.Success("获取成功", ls);
    }


  /*  @ApiOperation(value = "小程序--洗车注意事项", notes = "洗车注意事项", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/subText/getProblem", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap getProblem() {
        List<SubText> ls = subTextService.getProblem();
        if (ls == null || ls.size() == 0) {
            return ReturnUtil.Success("暂无内容");
        }
        return ReturnUtil.Success("获取成功", ls);
    }*/


    @ApiOperation(value = "管理端--用户权益", notes = "洗车注意事项", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/PartnerRight", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap partnerRight() {
        List<SubText> ls = subTextService.partnerRight();
        if (ls == null || ls.size() == 0) {
            return ReturnUtil.Success("暂无内容");
        }
        return ReturnUtil.Success("获取成功", ls);
    }




    @ApiOperation(value = "后端-获取所有", notes = "关于我们", produces = "application/json", response = SubText.class)
    @RequestMapping(value = "/v1/subText/getAll", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap getAll(HttpServletRequest request) {

        String sessionId = request.getHeader(Global.AUTHORIZATION);
        if(StringUtil.isNullOrEmpty(sessionId))
        {
            List<SubText> ls = subTextService.getAll();
            if (ls == null || ls.size() == 0) {
                return ReturnUtil.Success("暂无内容");
            }
            return ReturnUtil.Success("获取成功", ls);
        }
        UserInfo user=null;
        try{
            user = wxService.getUserInfoBySessionId(sessionId);
        }catch (Exception e)
        {
            List<SubText> ls = subTextService.getAll();
            if (ls == null || ls.size() == 0) {
                return ReturnUtil.Success("暂无内容");
            }
            return ReturnUtil.Success("获取成功", ls);

        }

//        UserInfo user = userInfoService.findById("57");
        List<SubText> ls = subTextService.getAll();
        if (ls == null || ls.size() == 0) {
            return ReturnUtil.Success("暂无内容");
        }
        Map map = new HashMap();
        Integer alert=0;
        Integer checked=0;
        if(!StringUtil.isNullOrEmpty(user))
        {
            if(!StringUtil.isNullOrEmpty(user.getAlert()))
            {
                alert=user.getAlert();
                checked=user.getAlert();
            }
        }
        map.put("alert",alert);
        map.put("checked",checked);
        return ReturnUtil.Success("获取成功", ls,map);
    }


    @ApiOperation(value = "后端-服务协议下次不再弹出", notes = "服务协议下次不再弹出", produces = "application/json", response = SubText.class)
    @RequestMapping(value = "/v1/subText/updateSubtextAlert", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap updateSubtextAlert(@RequestParam(value = "alert") Integer alert,HttpServletRequest request) {
        String sessionId = request.getHeader(Global.AUTHORIZATION);
        UserInfo user = wxService.getUserInfoBySessionId(sessionId);
        user.setAlert(alert);
        this.userInfoService.updateUser(user);
        return ReturnUtil.Success("修改成功");
    }




    @ApiOperation(value = "后端-新增", notes = "新增 类型 type 0.关于我们 1.洗车注意条款 2注册协议 ", produces = "application/json", response = SubText.class)
    @RequestMapping(value = "/v1/subText/add", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap add(@RequestBody @ApiParam SubText sub) {
        sub.setcTime(new Date());
        int changeRow = subTextService.add(sub);
        if (changeRow == 0) {
            return ReturnUtil.Error("添加失败");
        }
        return ReturnUtil.Success("添加成功", sub);
    }
    @Log("修改协议")
    @ApiOperation(value = "后端-修改", notes = "新增 类型 type 0.关于我们 1.洗车注意条款 2注册协议 ", produces = "application/json", response = SubText.class)
    @RequestMapping(value = "/v1/subText/update", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap update(@RequestBody @ApiParam SubText sub) {
        int changeRow = subTextService.update(sub);
        if (changeRow == 0) {
            return ReturnUtil.Error("修改失败");
        }
        return ReturnUtil.Success("修改成功", sub);
    }

    @Log("删除协议")
    @ApiOperation(value = "后端-删除", notes = "新增 类型 type 0.关于我们 1.洗车注意条款 2常见问题 ", produces = "application/json", response = SubText.class)
    @RequestMapping(value = "/v1/subText/delete", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap delete(@RequestParam String id) {
        int changeRow = subTextService.delete(id);
        if (changeRow == 0) {
            return ReturnUtil.Error("删除失败");
        }
        return ReturnUtil.Success("删除成功");
    }
}
